<?php
/**
 * ContactList.php
 * 
 * @category   Naneau
 * @package    Naneau_MSN
 * @copyright  Copyright (c) 2007 Maurice Fonk - http://naneau.nl
 * @version    0.1
 */

/**
 * Naneau_MSN_Messenger_ContactList
 * 
 * the contact list for use with the messenger
 *
 * @category   Naneau
 * @package    Naneau_MSN
 * @copyright  Copyright (c) 2007 Maurice Fonk - http://naneau.nl
 */
class Naneau_MSN_Messenger_ContactList implements SeekableIterator, Countable {

    /**
     * array of contacts
     *
     * @var array
     */
    private $_contacts = array();

    /**
     * pointer for iterator
     *
     * @var integer
     */
    private $_pointer = 0;

    /**
     * number of contacts
     *
     * @var integer
     */
    private $_count;

    /**
     * contacts
     *
     * @param array $contacts
     */
    public function __construct($contacts) {
        $this->_contacts = $contacts;
        //set contacts
        $this->_count = count($contacts);
        //set count
    }
    
    /**
     * add a contact
     *
     * @param Naneau_MSN_Messenger $contact
     */
    public function addContact($contact) {
        $this->_contacts[] = $contact;
        $this->_count++;
    }

    /**
     * get current contact
     *
     * @return Naneau_MSN_Messenger
     */
    public function current() {
        if ($this->valid() === false) {
            return null;
        }

        return $this->_contacts[$this->_pointer];
    }

    /**
     * get pointer
     *
     * @return int
     */
    public function key() {
        return $this->_pointer;
    }

    /**
     * move on, move on!
     *
     * @return void
     */
    public function next() {
        ++$this->_pointer;
    }

    /**
     * reset pointer to 0
     *
     * @return void
     */
    public function rewind() {
        $this->_pointer = 0;
    }

    /**
     * find a position
     *
     * @param int $index
     */
    public function seek ($index) {
        $this->_pointer = $index;

        if (!$this->valid()) {
            throw new OutOfBoundsException('Invalid seek position');
        }
    }

    /**
     * check if there's a valid pointer (so we can get a row)
     *
     * @return bool
     */
    public function valid()
    {
        return $this->_pointer < $this->_count;
    }

    /**
     * get the number of elements, for countable
     *
     * @return int
     */
    public function count()
    {
        return $this->_count;
    }
}